package com.yidu.hbats.service.impl.emp;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.dao.AtsEmpAthleteMapper;
import com.yidu.hbats.model.AtsEmpAthlete;
import com.yidu.hbats.service.emp.AtsEmpAthleteService;
import com.yidu.springbootdemo.permission.domain.Tuser;
import com.yidu.springbootdemo.permission.service.TuserService;
import com.yidu.springbootdemo.permission.tools.Tools;
import com.yidu.utils.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @program: hbats
 * @description: 运动员信息实现接口
 * @author: 张超
 * @create: 2019-10-29 18:54
 */

@Service
public class AtsEmpAthleteServiceImpl implements AtsEmpAthleteService {

    @Resource
    AtsEmpAthleteMapper atsEmpAthletedao;

    @Resource
    TuserService tuserService;


    @Override
    public int deleteAll(String ids) {
        if(StringUtils.isEmpty(ids) ){
            return 0;
        }else{
            String[] idss = ids.split(",");
            for(int i = 0;i<idss.length;i++){
                AtsEmpAthlete atsEmpAthlete = new AtsEmpAthlete();
                atsEmpAthlete.setAthleteId(Integer.valueOf(idss[i]));
                atsEmpAthlete.setIsva(2);
                saveOrUpdate(atsEmpAthlete);
            }
            return 1;
        }
    }

    @Override
    public List<AtsEmpAthlete> selectByCoachIdone(Integer coachId) {
        return atsEmpAthletedao.selectByCoachIdone(coachId);
    }

    @Override
    public List<AtsEmpAthlete> selectByCoachId(AtsEmpAthlete atsEmpAthlete) {
        return atsEmpAthletedao.selectByCoachId(atsEmpAthlete);
    }

    @Override
    public List<AtsEmpAthlete> selectTree() {
        return null;
    }

    @Override
    public int saveOrUpdate(AtsEmpAthlete atsEmpAthlete) {

        if(StringUtils.isEmpty(atsEmpAthlete.getAthleteId())){
            atsEmpAthletedao.insertSelective(atsEmpAthlete);

            Tuser tuser = new Tuser();
            String rd = UUID.randomUUID().toString().replaceAll("-", "");
            tuser.setUserId(rd);
            tuser.setFilId(atsEmpAthlete.getAthleteId().toString());//对应对象id
            tuser.setCompanyNum("4");//类型字段 对应 1 , 领导  2,教练员 3,专家 4,运动员
            tuser.setCompanyName(atsEmpAthlete.getAthleteName());//对象名称
            tuser.setCompany("运动员");//对象类型名称
            tuser.setUserName(atsEmpAthlete.getAthleteName());
            tuser.setUserPassword("123456");
            tuser.setUserState("Y");
            tuser.setUserFoundDate(Tools.time());
            tuser.setUserFoundUser(atsEmpAthlete.getCreater());


            tuserService.add(tuser);
        }else{
            atsEmpAthletedao.updateByPrimaryKeySelective(atsEmpAthlete);

            Tuser tuser = new Tuser();
            tuser.setFilId(atsEmpAthlete.getAthleteId().toString());
            tuser.setCompanyNum("4");

            Tuser tuser1 = tuserService.findBycompanyNum(tuser);

            if(tuser1!=null){
                //如果是删除的话 就把用户表中的也删除
                if(atsEmpAthlete.getIsva()!=null && atsEmpAthlete.getIsva()==2){
                    if(tuser1!=null){
                        tuserService.delete(tuser1.getUserId());
                    }
                }else{
                    tuser1.setCompanyName(atsEmpAthlete.getAthleteName());//对象名称
                    tuser1.setUserName(atsEmpAthlete.getAthleteName());
                    tuserService.update(tuser1);
                }
            }
        }

        return 0;
    }

    @Override
    public AtsEmpAthlete findById(Integer id) {
        return atsEmpAthletedao.selectByPrimaryKey(id);
    }

    @Override
    public List<AtsEmpAthlete> selectAll(AtsEmpAthlete atsEmpAthlete, Page page) {
        PageHelper.offsetPage(page.getStart(), page.getRows());

        List<AtsEmpAthlete> list = atsEmpAthletedao.selectAll(atsEmpAthlete);

        PageHelper.startPage(page.getStart(), page.getRows());
        return list;
    }

    @Override
    public int selectCount(AtsEmpAthlete atsEmpAthlete) {
        return atsEmpAthletedao.selectCount(atsEmpAthlete);
    }

    @Override
    public List<AtsEmpAthlete> findAll(AtsEmpAthlete atsEmpAthlete) {
        return atsEmpAthletedao.findAll(atsEmpAthlete);
    }

    @Override
    public List<AtsEmpAthlete> selectBysportId(AtsEmpAthlete atsEmpAthlete) {
        return atsEmpAthletedao.selectBysportId(atsEmpAthlete);
    }

    /**
     * 查询运动员与体育项目关联（remark字段暂时代替为体育项目名称）胡立增加
     * @return
     */
    @Override
    public List<AtsEmpAthlete> selectTree(Integer manId) {
        return atsEmpAthletedao.selectTree(manId);
    }
}